    <template>
    <!-- :style="{ background: variables.writeBg }" -->
    <div>
        <el-row>
            <el-col :span="12" class="hidden-xs-only">
                <div class="left_banner"></div>
            </el-col>
            <el-col :span="12" :xs="{ span: 22, offset: 1 }">
                <div class="right_banner">
                    <div class="login-container">
                        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">
                            <div class="title-container">
                                <div class="logo-box">
                                    <img :src="$store.state.settings.logo" style="width: 90px; height: 90px"/>
                                </div>
                                <h3 class="title">
                                    {{ this.$store.state.settings.title }}
                                </h3>
                            </div>

                            <el-form-item prop="username">
                                <span class="svg-container">
                                    <svg-icon icon-class="user"/>
                                </span>
                                <el-input
                                    ref="username"
                                    v-model="loginForm.username"
                                    placeholder="账号"
                                    name="username"
                                    type="text"
                                    tabindex="1"
                                    autocomplete="on"
                                />
                            </el-form-item>

                            <el-tooltip v-model="capsTooltip" content="大写锁定已开启" placement="right" manual>
                                <el-form-item prop="password">
                                    <span class="svg-container">
                                        <svg-icon icon-class="password"/>
                                    </span>
                                    <el-input
                                        :key="passwordType"
                                        ref="password"
                                        v-model="loginForm.password"
                                        :type="passwordType"
                                        placeholder="密码"
                                        name="password"
                                        tabindex="2"
                                        autocomplete="on"
                                        @keyup.native="checkCapslock"
                                        @blur="capsTooltip = false"
                                    />
                                    <span class="show-pwd" @click="showPwd">
                                        <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
                                    </span>
                                </el-form-item>
                            </el-tooltip>
                            <el-form-item style="display: flex; justify-content: flex-start; background: transparent">
                                <el-checkbox v-model="checked">自动登录</el-checkbox>
                            </el-form-item>
                            <el-form-item>
                                <el-button
                                    v-waves
                                    :loading="loading"
                                    size="medium"
                                    type="primary"
                                    style="width: 100%; height: 45px"
                                    @click.native.prevent="handleLogin"
                                >
                                    登录
                                </el-button>
                            </el-form-item>
                        </el-form>
                        <el-dialog title="第三方登录" :visible.sync="showDialog">
                            <br/>
                            <br/>
                            <br/>
                            <social-sign/>
                        </el-dialog>
                    </div>
                </div>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import SocialSign from './components/SocialSignin'
import PointWave from './components/pointWave'
import Logo from 'admin/layout/components/Sidebar/Logo.vue'
import waves from 'admin/directive/waves/index.js' // 水波纹指令
import variables from 'admin/styles/variables.scss'
import config from '/config'

export default {
    name: 'Login',
    directives: {
        waves,
    },
    components: {SocialSign, Logo, PointWave},
    computed: {
        variables() {
            return variables
        },
    },
    data() {
        const validatePassword = (rule, value, callback) => {
            if (value.length < 6) {
                callback(new Error('密码不能少于6位'))
            } else {
                callback()
            }
        }
        const validateCode = (rule, value, callback) => {
            if (value.length < 4) {
                callback(new Error('验证码不能少于4位'))
            } else {
                callback()
            }
        }
        return {
            checked: true,
            showLogo: true,
            isCollapse: true,
            loginForm: {
                // username: 'zhongshan',
                // password: 'zszx123456',
                username: config.isDev ? 'admin' : '',
                password: config.isDev ? 'admin' : '',
                // code: "",
            },
            loginRules: {
                username: [{required: true, trigger: 'blur', message: '用户名不能为空'}],
                password: [{required: true, trigger: 'blur',}],// validator: validatePassword
                // code: [{ required: true, trigger: "blur", validator: validateCode }],
            },
            url: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
            // banner: '/static/banner-bg.jpg',
            passwordType: 'password',
            capsTooltip: false,
            loading: false,
            showDialog: false,
            redirect: undefined,
            otherQuery: {},
        }
    },
    watch: {
        $route: {
            handler: function (route) {
                const query = route.query
                if (query) {
                    this.redirect = query.redirect
                    this.otherQuery = this.getOtherQuery(query)
                }
            },
            immediate: true,
        },
    },
    created() {
        addEventListener('keyup', ($event) => $event.key === 'Enter' && this.handleLogin()) //监听鼠标按下
    },
    mounted() {
        if (this.loginForm.username === '') {
            this.$refs.username.focus()
        } else if (this.loginForm.password === '') {
            this.$refs.password.focus()
        }
    },
    destroyed() {
        removeEventListener('keyup', (e) => {
        })
    },
    methods: {
        checkCapslock(e) {
            const {key} = e
            this.capsTooltip = key && key.length === 1 && key >= 'A' && key <= 'Z'
        },
        showPwd() {
            if (this.passwordType === 'password') {
                this.passwordType = ''
            } else {
                this.passwordType = 'password'
            }
            this.$nextTick(() => {
                this.$refs.password.focus()
            })
        },
        changeCode() {
            console.log('获取验证码')
        },
        async handleLogin() {
            this.$refs.loginForm.validate(async (valid) => {
                if (valid) {
                    this.loading = true
                    let flag = await this.$store.dispatch('user/login', this.loginForm)
                    if (flag) {
                        this.$notify({
                            message: '登录成功',
                            type: 'success',
                            duration: 2000,
                        })
                        this.$router.push({
                            path: "/",
                        });
                    }
                    // switch (this.loginForm.username) {
                    //     case "admin":
                    //         this.$router.push({
                    //             path: "/select_team",
                    //             query: this.loginForm,
                    //         });
                    //         break;
                    //     case "tempadmin":
                    //         this.$router.push({
                    //             path: "/select_team",
                    //             query: this.loginForm,
                    //         });
                    //         break;
                    //     case "zhongshan":
                    //         this.$store.dispatch("team/setCurrTeam", {
                    //             id: 11,
                    //             schoolName: "惠阳中山中学",
                    //             logo: "/static/admin/img/school_logo/zhong-shan-zhong-xue-logo.png",
                    //         });
                    //         this.$router.push({
                    //             path: "/",
                    //         });
                    //         break;
                    //     case "yizhongh":
                    //         this.$store.dispatch("team/setCurrTeam", {
                    //             id: 13,
                    //             schoolName: "惠州市惠阳区第一中学[高中部]",
                    //             logo: "/static/admin/img/school_logo/yi-zhong-logo.png",
                    //         });
                    //         this.$router.push({
                    //             path: "/",
                    //         });
                    //         break;
                    //     case "hywuzhong":
                    //         this.$store.dispatch("team/setCurrTeam", {
                    //             id: 9,
                    //             schoolName: "惠阳区第五中学",
                    //         });
                    //         this.$router.push({
                    //             path: "/",
                    //         });
                    //         break;
                    //     default:
                    //         this.$message.error("用户名或密码错误!");
                    //         break;
                    // }

                    this.loading = false
                } else {
                    console.log('error submit!!')
                    return false
                }
            })
        },
        getOtherQuery(query) {
            return Object.keys(query).reduce((acc, cur) => {
                if (cur !== 'redirect') {
                    acc[cur] = query[cur]
                }
                return acc
            }, {})
        },
        // afterQRScan() {
        //   if (e.key === 'x-admin-oauth-code') {
        //     const code = getQueryObject(e.newValue)
        //     const codeMap = {
        //       wechat: 'code',
        //       tencent: 'code'
        //     }
        //     const type = codeMap[this.auth_type]
        //     const codeName = code[type]
        //     if (codeName) {
        //       this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
        //         this.$router.push({ path: this.redirect || '/' })
        //       })
        //     } else {
        //       alert('第三方登录失败')
        //     }
        //   }
        // }
    },
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
        color: $cursor;
    }
}

.left_banner {
    height: 100vh;
    background: url('/static/admin/banner-bg.jpg') no-repeat 100%;
}

/* reset element-ui css */
.login-container {
    .el-input {
        // display: inline-block;
        height: 47px;
        width: 80%;

        input {
            background: transparent;
            border: 0px;
            -webkit-appearance: none;
            border-radius: 0px;
            padding: 12px 5px 12px 15px;
            color: $bg;
            height: 47px;
            caret-color: $bg;

            &:-webkit-autofill {
                box-shadow: 0 0 0px 1000px $bg inset !important;
                -webkit-text-fill-color: $cursor !important;
            }
        }
    }

    .el-form-item--mini .el-form-item__content {
        line-height: 28px;
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

    .el-form-item {
        width: 100%;
        border: 1px solid rgba(255, 255, 255, 0.1);
        background: rgba(0, 0, 0, 0.1);
        border-radius: 5px;
        color: #454545;
    }

    .el-input-group__append,
    .el-input-group__prepend {
        padding: 0;
    }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
    height: 100%;
    width: 100%;
    background-color: $writeBg;
    overflow: hidden;
    display: flex;
    justify-content: center;
    background-image: url('/static/admin/svg/bg.svg');
    background-repeat: no-repeat;
    background-position: center 260px;
    background-size: 100%;

    .login-banner {
        height: 100vh;
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    }

    .login-form {
        width: 380px;
        height: 100vh;
        max-width: 100%;
        padding-bottom: 50px;
        overflow: hidden;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
    }

    .tips {
        font-size: 14px;
        color: #fff;
        margin-bottom: 10px;

        span {
            &:first-of-type {
                margin-right: 16px;
            }
        }
    }

    .svg-container {
        padding: 6px 5px 6px 15px;
        color: $dark_gray;
        vertical-align: middle;
        width: 30px;
        display: inline-block;
    }

    .title-container {
        position: relative;

        .logo-box {
            height: 90px;
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .title {
            font-size: 30px;
            color: $themeColor;
            margin: 0px auto 40px auto;
            text-align: center;
            font-weight: bold;
            margin-top: 20px;
        }

        .set-language {
            color: #fff;
            position: absolute;
            top: 3px;
            font-size: 18px;
            right: 0px;
            cursor: pointer;
        }
    }

    .show-pwd {
        position: absolute;
        right: 10px;
        top: 7px;
        font-size: 16px;
        color: $dark_gray;
        cursor: pointer;
        user-select: none;
    }

    .img-code {
        position: absolute;
        right: 0;
        top: 0;
        color: $dark_gray;
        cursor: pointer;
        user-select: none;
    }

    .thirdparty-button {
        position: absolute;
        right: 0;
        bottom: 6px;
    }

    @media only screen and (max-width: 470px) {
        .thirdparty-button {
            display: none;
        }
    }
}
</style>
